// Copyright (C) 2024 Li Zhenhua
// Licence: GPLv2+

import {Extension} from 'resource:///org/gnome/shell/extensions/extension.js';
import * as Main from 'resource:///org/gnome/shell/ui/main.js';

export default class MoveClockExtension extends Extension {

 enable() {    
    let center = Main.sessionMode.panel.center;

    // do nothing if the clock isn't centred in this mode
    if ( center.indexOf('dateMenu') == -1 ) {
        return;
    }

    let centerBox = Main.panel._centerBox;
    let rightBox = Main.panel._rightBox;
    let dateMenu = Main.panel.statusArea['dateMenu'];
    let children = centerBox.get_children();

    // only move the clock if it's in the centre box
    if ( children.indexOf(dateMenu.container) != -1 ) {
        //Note: remove_actor deprecated, use remove_child instead.
        //https://gjs.guide/extensions/upgrading/gnome-shell-46.html#clutter-container
        centerBox.remove_child(dateMenu.container);

        children = rightBox.get_children();
        rightBox.insert_child_at_index(dateMenu.container, children.length-1);
   }
}

 disable() {
    let center = Main.sessionMode.panel.center;

    // do nothing if the clock isn't centred in this mode
    if ( center.indexOf('dateMenu') == -1 ) {
        return;
    }

    let centerBox = Main.panel._centerBox;
    let rightBox = Main.panel._rightBox;
    let dateMenu = Main.panel.statusArea['dateMenu'];
    let children = rightBox.get_children();

    // only move the clock back if it's in the right box
    if ( rightBox.get_children().indexOf(dateMenu.container) != -1 ) {
        rightBox.remove_child(dateMenu.container);
        centerBox.add_child(dateMenu.container);
    }
}

}

